<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>权限管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="权限名称/权限关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut" icon="el-icon-search">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()" icon="el-icon-circle-plus-outline">新建
                </el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" label="序号" align="center" width="100%"></el-table-column>
                <el-table-column prop="name" label="权限名称" align="center" width="200%"></el-table-column>
                <el-table-column prop="keyword" label="权限关键字" align="center" width="300%"></el-table-column>
                <el-table-column prop="description" label="权限描述" align="center" width="300%"></el-table-column>
                <el-table-column label="操作" align="center" width="400%">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)" icon="el-icon-edit">编辑
                        </el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)" icon="el-icon-delete">删除
                        </el-button>
                        <el-popover
                                placement="bottom"
                                width="150">
                            <el-table :data="roleData">
                                <el-table-column width="150" property="name" label="角色名"></el-table-column>
                            </el-table>
                            <el-button slot="reference" size="mini" type="info" @click="handleQueryRole(scope.row)"
                                       icon="el-icon-view">查看关联角色
                            </el-button>
                        </el-popover>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增检查项" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name" :error="errorMsg_name">
                                    <el-input v-model="formData.name" placeholder="请输入权限名称"
                                              prefix-icon="el-icon-circle-plus-outline" autocomplete="off"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限关键字" prop="keyword" :error="errorMsg">
                                    <el-input v-model="formData.keyword" placeholder="请输入权限关键字"
                                              prefix-icon="el-icon-circle-plus-outline" autocomplete="off"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑套餐 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名称" prop="name" :error="errorMsg_name">
                                    <el-input v-model="formData.name" prefix-icon="el-icon-edit" autocomplete="off"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限关键字" prop="keyword" :error="errorMsg">
                                    <el-input v-model="formData.keyword" prefix-icon="el-icon-edit" autocomplete="off"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--绑定角色页面-->
            <div class="add-form">
                <el-dialog title="绑定角色" :visible.sync="dialogFormVisibleUser" label-width="50px">
                    <div class="checkScrol">
                        <table class="datatable">
                            <thead>
                            <tr>
                                <th>选择</th>
                                <th>角色名称</th>
                                <th>角色说明</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr v-for="c in tableData">
                                <td>
                                    <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                </td>
                                <td><label :for="c.id">{{c.name}}</label></td>
                                <td><label :for="c.id">{{c.description}}</label></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleUser = false">取消</el-button>
                        <el-button type="primary" @click="handleEditUser()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            roleData: [],
            tableData: [],//回显用户数据
            roleIds: [],//用户id
            permissionId: null,
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            dialogFormVisibleUser: false,
            errorMsg: null,
            errorMsg_name: null,
            rules: {
                keyword: [
                    {required: true, message: '权限关键字为必填项', trigger: 'blur'}
                ],
                name: [
                    {required: true, message: '权限名称为必填项', trigger: 'blur'}
                ]
            }
        },
        created() {
            this.findPage();
        }
        ,
        methods: {
            //添加
            handleAdd() {
                this.errorMsg_name = null;
                this.errorMsg = null;
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        //1.验证（省略）
                        axios.post("/permissions/addVerifyThePermissionKeyword.do", this.formData).then(
                            res => {
                                if (!res.data.flag) {
                                    this.errorMsg = res.data.message;
                                    this.errorMsg_name = res.data.data;
                                } else {
                                    //2.提交表单数据
                                    axios.post("/permissions/addPermissions.do", this.formData).then(res => {
                                        this.resetForm();
                                        //弹出窗口
                                        this.dialogFormVisible = false;
                                        if (res.data.flag) {
                                            this.permissionId = res.data.data;
                                            this.$confirm('添加成功,是否将此权限与已有角色进行绑定？', '提示', {
                                                confirmButtonText: '确定',
                                                cancelButtonText: '取消',
                                                type: 'success'
                                            }).then(() => {
                                                this.dialogFormVisibleUser = true;
                                                axios.get("/usermanage/findAll.do").then(res => {
                                                    if (res.data.flag) {
                                                        this.tableData = res.data.data;
                                                    }
                                                })
                                            }).catch(() => {
                                                this.$message({
                                                    type: 'info',
                                                    message: '已取消绑定'
                                                });
                                            });
                                            this.findPage();//分页查询
                                        } else {
                                            this.message.error(res.data.message);
                                            this.findPage();//分页查询
                                        }
                                    })
                                }
                            });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,
            //绑定角色
            handleEditUser() {
                axios.post("/permissions/handleEditUser.do?roleIds="+this.roleIds, {
                    "permissionId": this.permissionId
                }).then(res => {
                    this.roleIds = [];
                    this.dialogFormVisibleUser = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                });
            },
            //分页查询
            findPage() {
                var parms = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }

                axios.post("/permissions/findPage.do", parms).then(res => {
                    //后台返回的是PageResult total rows
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            //编辑
            handleEdit() {
                this.errorMsg_name = null;
                this.errorMsg = null;
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        axios.post("/permissions/editVerifyThePermissionKeyword.do", this.formData).then(
                            res => {
                                if (!res.data.flag) {
                                    this.errorMsg = res.data.message;
                                    this.errorMsg_name = res.data.data;
                                } else {
                                    axios.post("/permissions/editPermissions.do", this.formData).then(res => {
                                        this.resetForm();
                                        //弹出窗口
                                        this.dialogFormVisible4Edit = false;
                                        this.$message({
                                            message: res.data.message,
                                            type: res.data.flag ? 'success' : 'error'
                                        });
                                        this.findPage();//分页查询
                                    })
                                }
                            }
                        );
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,
            handleUpdate(row) {
                //弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                //重置表单
                this.resetForm();
                // 回显权限
                axios.get("/permissions/findById.do?permissionsId=" + row.id).then(res => {
                    res.data == null ? this.message.error("回显权限失败！") : this.formData = res.data;//回显权限
                })
            },
            //删除套餐
            handleDelete(row) {
                this.$confirm('此操作将永久删除权限记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'info'
                }).then(() => {
                    //发送请求查询是否与角色有所关联
                    axios.get("/permissions/findPermissionsQueryRole.do?id=" + row.id).then(res => {
                        if (res.data) {
                            //删除权限
                            this.removePermissions(row);
                        } else {
                            this.$confirm('此权限已与角色有所关联(请谨慎删除), 再次确认是否删除?', '提示', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                //删除权限
                                this.removePermissions(row);
                            }).catch(() => {
                                this.$message({
                                    type: 'info',
                                    message: '已取消删除'
                                });
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //删除权限
            removePermissions(row) {
                axios.get("/permissions/deleteByPermissionsId.do?id=" + row.id).then(res => {
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    this.findPage();//分页查询
                })
            },
            //根据权限查询角色
            handleQueryRole(row) {
                axios.get("/permissions/handleQueryRole.do?permissionsId=" + row.id).then(
                    res => {
                        this.roleData = res.data;
                    }
                );
            },
            // 重置表单
            resetForm() {
                this.errorMsg_name = null;
                this.errorMsg = null;
                this.formData = {};
            }
            ,
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                //1.重置表单
                this.resetForm();
            }
            ,
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            }
        }
    })
</script>
</html>
